# coding:utf-8
import random
import time
from multiprocessing.managers import BaseManager
from UrlManager import UrlManager
from DataOutput import DataOutput
# import UrlManager

class NodeManager(object):

    def start_Manager(self, url_q, result_q):
        '''
        Create a dispatch Manager
        :param self: 
        :param url_q: 
        :param result_q: 
        :return: 
        '''

        BaseManager.register('get_task_queue', callable=lambda: url_q)
        BaseManager.register('get_result_queue', callable=lambda: result_q)

        manager = BaseManager(address=('127.0.0.1', 8111), authkey='baike')

        return manager

    def url_manager_proc(self, url_q, conn_q, root_url):
        url_manager = UrlManager()
        url_manager.add_new_url(root_url)
        while True:
            while (url_manager.has_new_url()):
                new_url = url_manager.get_new_url()
                url_q.put(new_url)
                print 'old_url=', url_manager.old_url_size()
                if (url_manager.old_url_size() > 20):
                    for i in range(0, 9):
                        url_q.put('end')

                    print 'Control Node send END notice!'
                    url_manager.save_progress('new_urls.txt', url_manager.new_urls)
                    url_manager.save_progress('old_urls.txt', url_manager.old_urls)
                    return

            try:
                if not conn_q.empty():
                    urls = conn_q.get()
                    url_manager.add_new_urls(urls)
            except BaseException, e:
                time.sleep(0.1)

    def result_solve_proc(self, result_q, conn_q, store_q):
        while (True):
            try:
                if not result_q.empty():
                    content = result_q.get(True)
                    if content['new_urls'] == 'end':
                        print 'result solve process receive end notice, then end!'
                        store_q.put('end')
                        return

                    conn_q.put(content['new_urls'])
                    store_q.put(content['data'])
                else:
                    time.sleep(0.1)
            except BaseManager, e:
                time.sleep(0.1)

    def store_proc(self, store_q):
        output = DataOutput()
        while True:
            if not store_q.empty():
                data = store_q.get()
                if data == 'end':
                    print 'store process receive end notice, then end!'
                    output.output_end(output.filepath)

                    return

                output.store_data(data)
            else:
                time.sleep(0.1)